<template>
  <div class="box">
    <div class="search">
      <label for=""
        ><input
          type="text"
          placeholder="搜索歌曲、歌手、专辑"
          v-model="Val"
          ref="input"
          @keyup.enter="search(Val)"
          @input="change(Val)"
        />
        <span @click="clock" v-if="clockShow"></span>
      </label>
    </div>

    <h6 class="hotSearch" v-if="show">热门搜索</h6>
    <ul class="hotList" v-if="show">
      <li v-for="(s, index) in hotList" :key="index" @click="search(s.first)">
        {{ s.first }}
      </li>
    </ul>

    <Loading v-if="hotList == null"></Loading>

    <ul class="resultList" v-if="clockShow">
      <li v-for="(g, index) in seachList" :key="g.id">
        <span>{{ index + 1 }}</span>
        <SongItem :item="g" :keywords="Val"></SongItem>
      </li>
      <Loading v-if="seachList == null"></Loading>
    </ul>

    <ul class="localList" v-if="!clockShow && localList.length > 0">
      <h3>历史记录</h3>
      <li v-for="(i, index) in localList" @click="search(i)" :key="index">
        {{ i }}
        <van-icon name="close" @click.stop="del(i)" />
      </li>
    </ul>
  </div>
</template>

<script>
import SongItem from "@/components/SongItem";
import Loading from "@/components/myLoading";
export default {
  data() {
    return {
      Val: "",
      hotList: null,
      seachList: null,
      show: true,
      clockShow: false,
      localList: [],
    };
  },
  components: {
    SongItem,
    Loading,
  },
  created() {
    this.$axios.get("/search/hot").then((data) => {
      this.hotList = data.data.result.hots;
    });
  },
  mounted() {
    this.localList = JSON.parse(localStorage.getItem("searchText")) || [];
  },
  methods: {
    del(i) {
      this.localList.splice(
        this.localList.indexOf(i),
        this.localList.indexOf(i) + 1
      );

      localStorage.setItem("searchText", JSON.stringify(this.localList));
    },
    change() {
      if (this.Val == "") {
        this.show = true;
        this.clockShow = false;
        this.Val = "";
        this.seachList = null;
      }
    },
    search(val) {
      if (val == " ") {
        return;
      }
      this.show = false;
      this.clockShow = true;
      this.Val = val;

      if (this.localList.indexOf(val.trim()) == -1) {
        if (val != "" && val != " ") {
          this.localList.unshift(val.trim());
          this.localList = this.localList.slice(0, 10);
          localStorage.setItem("searchText", JSON.stringify(this.localList));
        }
      }

      if (this.Val == "") {
        this.show = true;
        this.clockShow = false;
        this.Val = "";
        this.seachList = null;
      } else {
        let timeid = setTimeout(() => {
          if (timeid) clearTimeout(timeid);
          this.$axios.get("/search?keywords=" + val).then((data) => {
            this.seachList = data.data.result.songs;
          });
        }, 500);
      }
    },
    clock() {
      this.show = true;
      this.clockShow = false;
      this.Val = "";
      this.seachList = null;
    },
  },
};
</script>

<style lang="less" scoped>
.localList {
  margin-top: 20px;
  li {
    margin: 10px;
    border-bottom: 1px solid rgb(245, 245, 245);
    position: relative;
    .van-icon {
      position: absolute;
      right: 0px;
    }
  }
  h3 {
    // background: rgb(245, 245, 245);
    text-align: center;
    font-size: 18px;
  }
}
.box {
  .search {
    width: 100%;
    height: 70px;
    border-bottom: 1px solid rgb(220, 220, 220);
    display: flex;
    justify-content: center;
    align-items: center;
    label {
      position: relative;
      span {
        position: absolute;
        width: 16px;
        height: 16px;
        top: 7px;
        right: 15px;
        background-image: url("../assets/close.png");
        background-size: 16px;
        border-radius: 50%;
        cursor: pointer;
      }
      input {
        width: 95vw;
        height: 30px;
        outline: none;
        border-radius: 15px;
        padding-left: 20px;
        border: 1px solid transparent;
        // background-color: rgb(235, 236, 236);
        color: black;
      }
    }
  }
  .tip {
    padding-left: 2.5vw;
    color: royalblue;
    box-sizing: border-box;
  }
  .hotSearch {
    padding-left: 2.5vw;

    font-size: 12px;
    margin: 10px 0;
  }
  .hotList {
    display: flex;
    justify-content: space-around;
    align-content: flex-start;
    flex-wrap: wrap;

    li {
      border: 1px solid rgb(236, 237, 240);
      padding: 5px;
      margin: 5px;
      cursor: pointer;
      border-radius: 10px;
    }
  }
  .resultList {
    li {
      display: flex;
      &:first-of-type {
        span {
          color: yellow;
        }
      }
      &:nth-of-type(2) {
        span {
          color: #c0c0c0;
        }
      }
      &:nth-of-type(3) {
        span {
          color: rgb(233, 183, 124);
        }
      }

      div {
        flex: 1;
      }

      span {
        display: inline-block;
        line-height: 50px;
        margin: 0 8px;
        color: rgb(240, 240, 240);
      }
    }
  }
}
</style>